import {request} from "./index"
/** 添加购物车商品
 * 
 * @param {object} data
 * @param {string} data.skuId
 * @param {number} data.num
 * @param {string} data.cartType //购物车类型
 * @param {string} token
 * 
 */
const addCart = (data, token) => {
  return request.postFrom("v1/app/order/carts", data, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 清空购物车
 * 
 * @param {string} token
 */
const clearCart = (token) => {
  return request.delete("v1/app/order/carts", {}, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 获取购物车详情
 * 
 * @param {string} token
 */
const getCart = (token) => {
  return request.get("v1/app/order/carts/all", {}, {
    "Authorization": token
  })
}

/** 获取购物车结算详情
 * 
 * @param {string} way
 * @param {string} token
 */
const getCheckCart = (way, token) => {
  return request.get("v1/app/order/carts/checked", {
    way
  }, {
    "Authorization": token
  })
}

/** 删除一件商品
 * 
 * @param {array} skuIds
 * @param {string} token
 */
const delCart = (skuIds, token) => {
  return request.delete("v1/app/order/carts/sku/remove", {
    skuIds
  }, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 更新数量
 * 
 * @param {string} skuId
 * @param {number} num
 * @param {string} token
 */
const editCartNum = (skuId, num, token) => {
  return request.post(`v1/app/carts/sku/num/${skuId}`, {
    num
  }, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 全选/全不选
 * 
 * @param {boolean} checked	
 * @param {string} token
 */
const checkedAll = (checked, token) => {
  return request.post(`v1/app/carts/sku/checked`, {
    checked
  }, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 选中一个商品
 * 
 * @param {string} skuId	
 * @param {boolean} checked
 * @param {string} token
 */
const checkedCart = (skuId, checked, token) => {
  return request.post(`v1/app/carts/sku/checked/${skuId}`, {
    checked
  }, {
    "Authorization": token,
    "content-type": "application/x-www-form-urlencoded"
  })
}

/** 获取购物车可用优惠券数量
 * 
 * @param {string} way
 * @param {string} token
 */
const getCartCouponNum = (way, token) => {
  return request.get("v1/app/carts/coupon/num", {
    way
  }, {
    "Authorization": token
  })
}

/** 选择优惠券
 * 
 * @param {string} way
 * @param {string} memberCouponId
 * @param {boolean} used
 * @param {string} token
 */
const chooseCoupon = (way, memberCouponId, used, token) => {
  return request.get("v1/app/carts/select/coupon", {
    way,
    memberCouponId,
    used
  }, {
    "Authorization": token
  })
}

export {
  addCart,
  clearCart,
  getCart,
  getCheckCart,
  delCart,
  editCartNum,
  checkedAll,
  checkedCart,
  getCartCouponNum,
  chooseCoupon
}